using System;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using System.Data;
using cwcare.Modules.cwcareXML.Data;
using System.Linq;
using System.Web;
using System.Collections.Generic;


namespace cwcare.Modules.cwcareXML
{
    public partial class ViewMap : PortalModuleBase
    {
        public string lattitude;
        public string longitude;
        public string state;
        public string stateName;
        public string city;
        private mapsDataContext entityData = new mapsDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            entityData = new mapsDataContext();

            var states = (from a in entityData.cwc_Locations
                          select a.state).Distinct().ToList();


            var maps = (from a in entityData.cwc_States
                        where states.Contains(a.StateCode)
                        select a);

            rpMaps.DataSource = maps;
            rpMaps.DataBind();
        }

        public string SetStateClass(object obj)
        {
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return string.Concat("class=\"{linked:", obj.ToString(), "}\"");
            }
            return null;
        }

        public void btViewMap_Click(object sender, EventArgs e)
        {
            state = hfLattitude.Value;
            stateName = (from a in entityData.cwc_States
                         where a.StateCode == state
                         select a.StateName).FirstOrDefault();

            var locations = (from a in entityData.cwc_Locations
                             where a.state == state
                             group a by a.city into g
                             select new
                             {
                                 City = g.FirstOrDefault().city,
                                 Lattitude = g.FirstOrDefault().lattitude,
                                 Longitude = g.FirstOrDefault().longitude
                             });//.FirstOrDefault();

            rplocations.DataSource = locations;
            rplocations.DataBind();

            var entity = locations.FirstOrDefault();
            lattitude = entity.Lattitude;
            longitude = entity.Longitude;
            mltView.SetActiveView(vDetail);

        }

        public void btnViewCity_Click(object sender, EventArgs e)
        {
            city = hfCity.Value;
            var locations = (from a in entityData.cwc_Locations
                             join c in entityData.cwc_Location_Providers on a.addressID equals c.addressID
                             join p in entityData.cwc_Providers on c.providerID equals p.providerID
                             where a.city == city
                             orderby p.lName
                             select new
                             {
                                 lattitude = a.lattitude,
                                 longitude = a.longitude,
                                 address = a.address,
                                 phone = a.phone,
                                 providerID = p.providerID,
                                 providerName = p.fName + " " + p.lName,
                             });

            rpAddress.DataSource = locations;
            rpAddress.DataBind();
            var entity = locations.FirstOrDefault();
            if (entity != null)
            {
                HttpCookie cookies = new HttpCookie("lattitude", string.Concat(entity.lattitude, ":", entity.longitude));
                cookies.Path = "/";
                HttpContext.Current.Response.SetCookie(cookies);
                lattitude = entity.lattitude;
                longitude = entity.longitude;
            }
            mltView.SetActiveView(vShowMap);
        }
    }
}